# -*- coding: utf-8 -*-
# @Time : 2022/6/20 下午2:05
# @Author : wyf
# @FileName: 作业7.py
# @Email: yunfei.wang@innvote.com
import base64
import hashlib
import json
import time

import requests


def md5_test(udid, time_str):
    # 创建md5对象
    hl = hashlib.md5()
    # Tips
    sss = f"{udid}app@cdsb!^jlx%2386*hip${time_str}"
    # 此处必须声明encode
    hl.update(sss.encode(encoding='utf-8'))
    # return hl.hexdigest()
    return base64.b64encode(hl.hexdigest().encode("utf-8")).decode("utf-8")


def get_headers():
    time_str = int(time.time()*1000)
    udid = "60ed4f4d-8023-4a32-b904-9be677329991"
    """"""
    device_info = {
        "Model": "vivo X6D",
        "Brand": "vivo",
        "OS": "Android 5.1",
        "OSDetail": "5.1",
        "Udid": udid,
        "Token": "0",
        "Timestamp": time_str,
        "UserId": -1,
        "Secret": md5_test(udid, time_str),
        "Version": "V6.9.4",
        "VersionCode": "2020071401",
        "JPushRegId": ""
    }

    _headers = {
        "DeviceInfo": json.dumps(device_info),
        "Content-Type": "application/json; charset=UTF-8",
        "Content-Length": "35",
        "Host": "app.cdsb.com",
        "Connection": "Keep-Alive",
        "Accept-Encoding": "gzip",
        "Cookie": "acw_tc=2f61f27716557080608866298e474a8bf4454cc1372c9c10b24c89621beea3",
        "User-Agent": "okhttp/3.12.0",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
        "POST": "/cdsb_app/api3/GetICListData HTTP/1.1",
    }
    return _headers


def spider(page: int):
    url = "https://app.cdsb.com/cdsb_app/api3/GetICListData"
    _data = json.dumps({"cId": 1, "timestamp": 0, "pageNum": page})
    req = requests.post(url=url,  data=_data,  headers=get_headers())
    code = req.json().get("Code")
    if code == 0:
        return req.json().get("Data").get("List")
    else:
        print("爬取信息错误")
        return None


if __name__ == '__main__':
    for i in range(1, 11):
        print(f"获取第 {i} 页数据~~")
        for news in spider(i):
            print(news)

